package com.xiaoxing.salesclient.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseLazyFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoxing.salesclient.di.component.DaggerChanPinJiaGeTabComponent;
import com.xiaoxing.salesclient.di.module.ChanPinJiaGeTabModule;
import com.xiaoxing.salesclient.mvp.contract.ChanPinJiaGeTabContract;
import com.xiaoxing.salesclient.mvp.model.entity.ProductList;
import com.xiaoxing.salesclient.mvp.presenter.ChanPinJiaGeTabPresenter;
import com.xiaoxing.salesclient.mvp.ui.adapter.ChanPinJiaGeTabAdapter;
import com.xiaoxing.salesclient.mvp.ui.inter.JiaMu;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.armscomponent.commonsdk.core.BaseConstants;
import xiaoxing.com.salesclient.R;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class ChanPinJiaGeTabFragment extends BaseLazyFragment<ChanPinJiaGeTabPresenter> implements ChanPinJiaGeTabContract.View, OnRefreshListener, JiaMu {


    public static final String TYPE_ID = "type_id";
    private static boolean mIsNeedDemo = true;

    TextView empty_text;

    ImageView empty_image;

    View mEmptyLayout;

    RecyclerView mRecyclerView;

    RefreshLayout mRefreshLayout;
    private ChanPinJiaGeTabAdapter mAdapter;
    private String mTypeId = "";
    private List<ProductList.DataBean> mDataBeanList = new ArrayList<>();
    public static JiaMu mJiaMu;

    @Override
    public void chanPinJiaMu(String type_id) {

        productList(type_id);
    }

    @Override
    public void liaoChengJiaMU(String type_id) {
        medproList(type_id);
    }


    public static void setCallBack(JiaMu jiaMu) {
        mJiaMu = jiaMu;
    }

    public static ChanPinJiaGeTabFragment newInstance(String type_id) {
        ChanPinJiaGeTabFragment fragment = new ChanPinJiaGeTabFragment();
        Bundle args = new Bundle();
        args.putString(TYPE_ID, type_id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerChanPinJiaGeTabComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .chanPinJiaGeTabModule(new ChanPinJiaGeTabModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_chan_pin_jia_ge_tab, null);
        setCallBack(this);
        getTypeId();


        mRefreshLayout = (RefreshLayout) view.findViewById(R.id.refreshLayout);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        mEmptyLayout = (View) view.findViewById(R.id.empty);
        empty_text = (TextView) view.findViewById(R.id.empty_text);
        empty_image = (ImageView) view.findViewById(R.id.empty_image);

        initRefreshLayout();
        initRecyclerView();
        initEmpty();

        return view;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        productList(mTypeId);
    }

    private void getTypeId() {
        mTypeId = getArguments().getString(TYPE_ID);
    }

    private void productList(String type_id) {
        mEmptyLayout.setVisibility(View.VISIBLE);
        mDataBeanList.clear();
        mDataBeanList.addAll(new ArrayList<>());
        mAdapter.notifyDataSetChanged();
        mPresenter.productList(type_id, mSharedPreferencesHelper.getString(BaseConstants.SHOP_ID));
    }

    private void medproList(String type_id) {
        mEmptyLayout.setVisibility(View.VISIBLE);
        mDataBeanList.clear();
        mDataBeanList.addAll(new ArrayList<>());
        mAdapter.notifyDataSetChanged();
        mPresenter.medproList(type_id, mSharedPreferencesHelper.getString(BaseConstants.SHOP_ID));
    }

    private void initRefreshLayout() {
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.public_colorPrimary).setAccentColorId(android.R.color.white));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.setEnableLoadMore(false);
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), VERTICAL));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new ChanPinJiaGeTabAdapter(getActivity(), mDataBeanList));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Utils.navigation(getActivity(), RouterHub.SALES_CLIENT_ZUI_XIN_XIAO_XI_XIANG_QING_ACTIVITY);
            }
        });
    }

    private void initEmpty() {
        empty_image.setImageResource(R.drawable.no_data);
        empty_text.setTextColor(getResources().getColor(R.color.public_no_data));
        empty_text.setText("暫無消息,下拉更新");
    }


    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
        mRefreshLayout.finishRefresh();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }


    @Override
    public void productListSuccessed(ProductList productList) {

        if (productList.getData() != null && productList.getData().size() > 0) {
            mEmptyLayout.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.VISIBLE);
            mDataBeanList.clear();
            mDataBeanList.addAll(productList.getData());
            mAdapter.notifyDataSetChanged();
        } else {
            mEmptyLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        }

    }

    @Override
    public void medproListSuccessed(ProductList productList) {

        if (productList.getData() != null && productList.getData().size() > 0) {
            mEmptyLayout.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.VISIBLE);
            mDataBeanList.clear();
            mDataBeanList.addAll(productList.getData());
            mAdapter.notifyDataSetChanged();
        } else {
            mEmptyLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);

        }
    }

    @Override
    protected void loadData() {
    }


}
